﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.WindowsMobile.PocketOutlook;
using Microsoft.WindowsMobile.Forms;
using System.Collections;


namespace GWHandCalendarManilaSettingTool
{
    public partial class Form_main : Form
    {
        public PictureBox[] cp = new PictureBox[18];
        public TextBox[] cn = new TextBox[18];
        public TextBox[] ct = new TextBox[18];

        public Panel selectedContact = new Panel();
        public PictureBox selectedContactPhoto = new PictureBox();
        public TextBox selectedContactName = new TextBox();
        public ComboBox selectedContactTel = new ComboBox();
        public Button selectedContactOK = new Button();
        public Button selectedContactCancel = new Button();

        public Bitmap defaultContactPhoto = new Bitmap(GloV.exePath + @"TodaySkins\Manila\contact\default.png");

        public Label userN1 = new Label();
        public Label userT1 = new Label();

        //public PictureBox[] c1p = new PictureBox[6];
        //public PictureBox[] c2p = new PictureBox[6];
        //public PictureBox[] c3p = new PictureBox[6];

        //public Label[] c1n = new Label[6];
        //public Label[] c2n = new Label[6];
        //public Label[] c3n = new Label[6];

        //public Label[] c1t = new Label[6];
        //public Label[] c2t = new Label[6];
        //public Label[] c3t = new Label[6];

        ListView c1INIList = new ListView();
        ListView c2INIList = new ListView();
        ListView c3INIList = new ListView();

        ListView p1INIList = new ListView();
        ListView p2INIList = new ListView();
        ListView p3INIList = new ListView();
        public Label testLable = new Label();
        
        public Form_main()
        {
            InitializeComponent();
            //初始化面版
            initContactSpace();

            initProgramIconSpace();

            initSelectedContact();

            initSelectedProgram();

            string contactPath = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase.Substring(0,
                System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase.LastIndexOf(@"\") + 1) + @"TodaySkins\Contact.ini";


            
            StreamReader objreader = new StreamReader(@contactPath, Encoding.Default);

            string sline = "";
            ArrayList al = new ArrayList();
            while (sline != null)
            {
                sline = objreader.ReadLine();
                if (sline != null)
                {
                    ListViewItem lvit = new ListViewItem();
                    lvit.Text = sline;
                    if (sline.LastIndexOf(",") != -1)
                    {
                        if (sline.Substring(0, sline.LastIndexOf(",")) == "DrawStringEx=10,60,70,480,0,#ffffff,400")
                        {
                            lvit.SubItems.Add("c1Title");
                            tabPage_c1.Text = sline.Substring(sline.LastIndexOf(",") + 1);
                            //MessageBox.Show(lvit.SubItems[1].Text.ToString());
                        }
                        else
                        {
                            lvit.SubItems.Add("");
                        }
                    }
                    
                    c1INIList.Items.Add(lvit);
                    al.Add(sline);
                }
            }
            objreader.Close();

            //MessageBox.Show(c1INIList.Items[10].Text);

            StreamWriter sw = new StreamWriter(@contactPath, false, Encoding.Default);
            foreach (string s in al)
            {
                sw.WriteLine(s);
                textBox1.Text += s + "\r\n";
            }
            sw.Close();


           



            testLable = new Label();
            testLable.Name = "testLable1";
            testLable.Text = "testL1";
            testLable.Size = new Size(300, 30);
            testLable.Location = new Point(0, 0);
            tabPage_c1.Controls.Add(testLable);

            testLable.Text = testLable.Name;

            //testLable = new Label();
            //testLable.Name = "testLable2";
            //testLable.Text = "testL2";
            //testLable.Location = new Point(0, 0);
            //tabPage_c2.Controls.Add(testLable);
            //通过控件名获得实例
            //Label tabpaget = new Label();
            //System.Reflection.FieldInfo fieldInfo = this.GetType().GetField("testLable1");
            //tabpaget = (Label)fieldInfo.GetValue(this);
            //tabpaget.Text = "OK";



            //tabpaget = (Label)fieldInfo.GetValue(tabPage_c3);
            //tabpaget.Text = "XXX1";
            //fieldInfo = this.GetType().GetField("testLable2");
            //tabpaget = (Label)fieldInfo.GetValue(this);
            //tabpaget.Text = "XXX2";

        }

        private void initContactSpace()
        {
            int forciT = 0;

            for (int ci = 0; ci < 18; ci++)
            {
                cp[ci] = new PictureBox();
                cp[ci].Name = "cp" + ci.ToString();
                cp[ci].Tag = "default.png";
                cp[ci].Size = new Size(100, 100);
                cp[ci].Parent = tabPage_c1;
                cp[ci].SizeMode = PictureBoxSizeMode.StretchImage;
                cp[ci].BackColor = Color.SkyBlue;
                if (ci >= 0 && ci < 6)
                {
                    forciT = ci;
                }
                else if (ci >= 6 && ci < 12)
                {
                    forciT = ci - 6;
                }
                else if (ci >= 12 && ci < 18)
                {
                    forciT = ci - 12;
                }
                cp[ci].Location = new Point(46 + (forciT < 3 ? forciT : forciT - 3) * 146, forciT < 3 ? 184 : 370);
                cp[ci].Click += new System.EventHandler(this.cp_Click);

                cn[ci] = new TextBox();
                cn[ci].Name = "cn" + ci.ToString();
                cn[ci].Size = new Size(100, 25);
                cn[ci].Parent = tabPage_c1;
                //cn[ci].TextAlign = ContentAlignment.TopCenter;
                cn[ci].TextAlign = HorizontalAlignment.Center;
                cn[ci].BorderStyle = BorderStyle.None;
                cn[ci].Font = new Font(FontFamily.GenericSerif, 8f, FontStyle.Bold);
                cn[ci].Location = new Point(cp[ci].Location.X, cp[ci].Location.Y + cp[ci].Height);
                cn[ci].Text = "姓名" + ci.ToString();

                ct[ci] = new TextBox();
                ct[ci].Name = "ct" + ci.ToString();
                ct[ci].Size = new Size(140, 25);
                ct[ci].Parent = tabPage_c1;
                //ct[ci].TextAlign = ContentAlignment.TopCenter;
                ct[ci].TextAlign = HorizontalAlignment.Center;
                ct[ci].BorderStyle = BorderStyle.None;
                ct[ci].Font = new Font(FontFamily.GenericSerif, 7f, FontStyle.Bold);
                ct[ci].Location = new Point(cn[ci].Location.X - 20, cn[ci].Location.Y + cn[ci].Height);
                ct[ci].Text = "电话号码";

                if (ci >= 0 && ci < 6)
                {
                    tabPage_c1.Controls.Add(cp[ci]);
                    tabPage_c1.Controls.Add(cn[ci]);
                    tabPage_c1.Controls.Add(ct[ci]);
                }
                else if (ci >= 6 && ci < 12)
                {
                    tabPage_c2.Controls.Add(cp[ci]);
                    tabPage_c2.Controls.Add(cn[ci]);
                    tabPage_c2.Controls.Add(ct[ci]);
                }
                else if (ci >= 12 && ci < 18)
                {
                    tabPage_c3.Controls.Add(cp[ci]);
                    tabPage_c3.Controls.Add(cn[ci]);
                    tabPage_c3.Controls.Add(ct[ci]);
                }
            }

        }

        private void initProgramIconSpace()
        {
            //---
        }

        private void initSelectedContact()
        {
            selectedContact.Size = new Size(360, 280);
            selectedContact.Location = new Point(60, 100);
            selectedContact.BackColor = Color.LightGray;
            selectedContact.Hide();

            selectedContactPhoto.Size = new Size(100, 100);
            selectedContactPhoto.Location = new Point(50, 60);
            selectedContactPhoto.SizeMode = PictureBoxSizeMode.StretchImage;
            selectedContactPhoto.BackColor = Color.SkyBlue;
            selectedContactPhoto.Tag = "";
            

            selectedContactName.Size = new Size(150, 25);
            selectedContactName.TextAlign = HorizontalAlignment.Center;
            selectedContactName.BorderStyle = BorderStyle.None;
            selectedContactName.Font = new Font(FontFamily.GenericSerif, 8f, FontStyle.Bold);
            selectedContactName.Location = new Point(160,60);
            selectedContactName.Text = "";

            selectedContactTel.Width = 150;
            selectedContactTel.Font = new Font(FontFamily.GenericSerif, 8f, FontStyle.Bold);
            selectedContactTel.Location = new Point(160, 95);

            selectedContactOK.Size = new Size(80, 30);
            selectedContactOK.Location = new Point(80, 200);
            selectedContactOK.Text = "确定";
            selectedContactOK.Click += new System.EventHandler(selectedContactOK_Click);

            selectedContactCancel.Size = new Size(80, 30);
            selectedContactCancel.Location = new Point(200, 200);
            selectedContactCancel.Text = "取消";
            selectedContactCancel.Click += new System.EventHandler(selectedContactCancel_Click);

            selectedContact.Controls.Add(selectedContactPhoto);
            selectedContact.Controls.Add(selectedContactName);
            selectedContact.Controls.Add(selectedContactOK);
            selectedContact.Controls.Add(selectedContactCancel);
            selectedContact.Controls.Add(selectedContactTel);

            this.Controls.Add(selectedContact);
            tabControl1.SendToBack();

        }

        private void initSelectedProgram()
        {
            //---
        }

        private void cp_Click(object sender, EventArgs e)
        {
            tabControl1.Enabled = false;
            ChooseContactDialog chooseCD = new ChooseContactDialog();
            chooseCD.HideNew = false;
            chooseCD.Title = "选择要显示到[" + ((PictureBox)sender).Parent.Text + "]的联系人";
            if (chooseCD.ShowDialog() == DialogResult.OK)
            {
                selectedContactName.Text = chooseCD.SelectedContact.FileAs;

                selectedContactTel.Items.Clear();

                if (chooseCD.SelectedContact.MobileTelephoneNumber != "")
                {
                    selectedContactTel.Items.Add(chooseCD.SelectedContact.MobileTelephoneNumber);
                }
                if (chooseCD.SelectedContact.HomeTelephoneNumber != "")
                {
                    selectedContactTel.Items.Add(chooseCD.SelectedContact.HomeTelephoneNumber);
                }
                if (chooseCD.SelectedContact.Home2TelephoneNumber != "")
                {
                    selectedContactTel.Items.Add(chooseCD.SelectedContact.Home2TelephoneNumber);
                }
                if (chooseCD.SelectedContact.BusinessTelephoneNumber != "")
                {
                    selectedContactTel.Items.Add(chooseCD.SelectedContact.BusinessTelephoneNumber);
                }
                if (chooseCD.SelectedContact.Business2TelephoneNumber != "")
                {
                    selectedContactTel.Items.Add(chooseCD.SelectedContact.Business2TelephoneNumber);
                }
                if (chooseCD.SelectedContact.CarTelephoneNumber != "")
                {
                    selectedContactTel.Items.Add(chooseCD.SelectedContact.CarTelephoneNumber);
                }
                if (chooseCD.SelectedContact.RadioTelephoneNumber != "")
                {
                    selectedContactTel.Items.Add(chooseCD.SelectedContact.RadioTelephoneNumber);
                }

                if (selectedContactTel.Items.Count > 0)
                {
                    selectedContactTel.SelectedIndex = 0;
                }

                if (chooseCD.SelectedContact.Picture != null)
                {

                    Rectangle oldrct;
                    Bitmap bmp;
                    bmp = (Bitmap)chooseCD.SelectedContact.Picture;
                    oldrct = new Rectangle(0, 0, bmp.Width, bmp.Height);
                    Bitmap tmpbmp = null;
                    tmpbmp = new Bitmap(100, 100);
                    Graphics g = Graphics.FromImage(tmpbmp);

                    int nr_x, nr_y, nr_w, nr_h;
                    if (bmp.Width >= bmp.Height)
                    {
                        nr_w = 100;
                        nr_x = 0;
                        nr_h = Convert.ToInt16((100f /((float)( bmp.Width))) * ((float)(bmp.Height)));
                        nr_y = (100 - nr_h) / 2;
                    }
                    else
                    {
                        nr_h = 100;
                        nr_y = 0;
                        nr_w = Convert.ToInt16((100f / ((float)(bmp.Height))) * ((float)(bmp.Width)));
                        nr_x = (100 - nr_w) / 2;
                    }

                    Rectangle newrct = new Rectangle(nr_x, nr_y, nr_w, nr_h);

                    SolidBrush blackBrush = new SolidBrush(Color.Black);
                    g.FillRectangle(blackBrush, new Rectangle(0, 0, tmpbmp.Width, tmpbmp.Height));
                    g.DrawImage(bmp, newrct, oldrct, GraphicsUnit.Pixel);
                    Pen tempPen = new Pen(Color.DimGray, 1f);
                    g.DrawRectangle(tempPen, new Rectangle(0, 0, 99, 99));
                    tempPen.Color = Color.Silver;
                    g.DrawRectangle(tempPen, new Rectangle(1, 1, 97, 97));
                    selectedContactPhoto.Image = tmpbmp;
                    g.ResetClip();







                    //selectedContactPhoto.Image = chooseCD.SelectedContact.Picture;
                    selectedContactPhoto.Tag = chooseCD.SelectedContact.ItemId;
                }
                else
                {
                    selectedContactPhoto.Image = defaultContactPhoto;
                    selectedContactPhoto.Tag = "default.png";
                }

                selectedContact.Tag = ((PictureBox)sender).Name;
                selectedContact.Show();
            }
        }

        private void selectedContactOK_Click(object sender, EventArgs e)
        {
            int contactIndex = Convert.ToInt16(selectedContact.Tag.ToString().Substring(2));
            cp[contactIndex].Image = selectedContactPhoto.Image;
            cn[contactIndex].Text = selectedContactName.Text;
            ct[contactIndex].Text = selectedContactTel.Text;

            if (selectedContactPhoto.Tag.ToString() == "default.png")
            {
                cp[contactIndex].Tag = selectedContactPhoto.Tag;
            }
            else
            {
                selectedContactPhoto.Image.Save(GloV.exePath + @"TodaySkins\Manila\contact\" + selectedContactPhoto.Tag.ToString() + ".jpg", ImageFormat.Jpeg);
                cp[contactIndex].Tag = selectedContactPhoto.Tag.ToString() + ".jpg";
            }

            selectedContact.Hide();
            tabControl1.Enabled = true;
        }

        private void selectedContactCancel_Click(object sender, EventArgs e)
        {
            selectedContact.Hide();
            tabControl1.Enabled = true;
        }













        private void menuItem3_Click(object sender, EventArgs e)
        {
            closeApp();
        }

        private void closeApp()
        {
            this.Close();
        }

        private void DrawImageRect4IntAtrrib(PaintEventArgs e)
        {

            // Create image.

            Bitmap bb = new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase.Substring(0, System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase.LastIndexOf(@"\") + 1) + @"x.jpg");
            Image newImage = (Image)bb;


            // Create rectangle for displaying original image.
            Rectangle destRect1 = new Rectangle(10, 20, 100, 200);

            // Create coordinates of rectangle for source image.
            int x = 0;
            int y = 0;
            int width = 800;
            int height = 800;
            GraphicsUnit units = GraphicsUnit.Pixel;

            // Draw original image to screen.
            e.Graphics.DrawImage(newImage, destRect1, new Rectangle(x, y, width, height), units);
            //e.Graphics.DrawImage(newImage, destRect1, x, y, width, height, units);

            // Create rectangle for adjusted image.
            Rectangle destRect2 = new Rectangle(10, 175, 200, 200);

            // Create image attributes and set large gamma.
            ImageAttributes imageAttr = new ImageAttributes();



            Bitmap ww = new Bitmap(500, 500);
            PictureBox www = new PictureBox();
            this.CreateGraphics().DrawImage(newImage, destRect2, x, y, width, height, units, imageAttr);
            www.Width = 800;
            www.Height = 800;
            www.Image = bb;
            www.SizeMode = PictureBoxSizeMode.StretchImage;
            www.Scale(new SizeF(0.1f, 0.1f));
            www.Location = new Point(10, 400);

            this.Controls.Add(www);






            Rectangle oldrct;
            Bitmap bmp;
            bmp = (Bitmap)www.Image;
            oldrct = new Rectangle(0, 0, bmp.Width, bmp.Height);
            www.Image = bmp;
            Bitmap tmpbmp = null;
            tmpbmp = new Bitmap(100, 100);
            Graphics g = Graphics.FromImage(tmpbmp);
            Rectangle newrct = new Rectangle(0, 25, tmpbmp.Width, tmpbmp.Height / 2);

            //SolidBrush blueBrush = new SolidBrush(Color.Blue);
            SolidBrush blackBrush = new SolidBrush(Color.Black);
            g.FillRectangle(blackBrush, new Rectangle(0, 0, tmpbmp.Width, tmpbmp.Height));

            g.DrawImage(bmp, newrct, oldrct, GraphicsUnit.Pixel);
            Pen tempPen = new Pen(Color.White, 2f);
            g.DrawRectangle(tempPen, new Rectangle(0, 0, tmpbmp.Width - Convert.ToInt16(tempPen.Width / 2), tmpbmp.Height - Convert.ToInt16(tempPen.Width / 2)));
            www.Image = tmpbmp;
            www.Size = new Size(tmpbmp.Width, tmpbmp.Height);
            g.ResetClip();

            www.Image.Save(@"\Program Files\GWHandCalendarManilaSettingTool\test_1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);


            bmp = ExtractIcon.GetBitmapFromExeIcon(@"\Program Files\WM6GroupContact1\WM6GroupContact1.exe");
            tmpbmp = null;
            tmpbmp = new Bitmap(76, 76);
            g = Graphics.FromImage(tmpbmp);
            blackBrush.Color = Color.White;
            g.FillRectangle(blackBrush, new Rectangle(0, 0, tmpbmp.Width, tmpbmp.Height));
            Bitmap iconBG = new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase.Substring(0,
                System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase.LastIndexOf(@"\") + 1) + @"\TodaySkins\Manila\program\programIconBase.jpg");
            g.DrawImage(iconBG, 0, 0);
            newrct = new Rectangle(0, 0, tmpbmp.Width, tmpbmp.Height);
            oldrct = new Rectangle(0, 0, bmp.Width, bmp.Height);
            //g.DrawImage(bmp, newrct, oldrct, GraphicsUnit.Pixel);
            ExtractIcon.DrawImageTransparent(g, bmp, newrct);

            //tempPen = new Pen(Color.Yellow, 2f);
            //g.DrawRectangle(tempPen, new Rectangle(0, 0, tmpbmp.Width - Convert.ToInt16(tempPen.Width / 2), tmpbmp.Height - Convert.ToInt16(tempPen.Width / 2)));

            pictureBox_icon.Image = tmpbmp;
            pictureBox_icon.Size = new Size(tmpbmp.Width, tmpbmp.Height);
            g.ResetClip();
            //pictureBox_icon.Image
            tmpbmp.Save(@"\Program Files\GWHandCalendarManilaSettingTool\testIcon.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);


        }

        private void Form_main_Paint(object sender, PaintEventArgs e)
        {
            DrawImageRect4IntAtrrib(e);
        }

        private void button_chooseContact_Click(object sender, EventArgs e)
        {
            ChooseContactDialog chooseCD = new ChooseContactDialog();
            chooseCD.HideNew = true;
            chooseCD.Title = "选择要显示到[" + tabPage_c1.Text.Trim() + "]组位置[" + "1" + "]的联系人";
            chooseCD.ShowDialog();
            //MessageBox.Show(chooseCD.SelectedContact.ItemId.ToString());
            if (chooseCD.SelectedContact.Picture != null)
            {
                cp[0].Image = chooseCD.SelectedContact.Picture;
            }
            cn[0].Text = chooseCD.SelectedContact.FileAs.ToString();
            cn[0].Tag = chooseCD.SelectedContact.ItemId;
            ct[0].Text = chooseCD.SelectedContact.MobileTelephoneNumber.ToString();
        }

    }
}